package lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Classname Print
 * @Description 交替打印奇偶数
 * @Date 2020/2/16 1:13 下午
 * @Created by gule
 */
public class PrintByVolatile {

    private volatile static int count = 0;
    private volatile static boolean flag = true;
    private static Integer defaultValue = 1000;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.execute(()->{
            while (count < defaultValue){
                if(flag) {
                    System.out.println("num is " + count);
                    count++;
                    flag = false;
                }
            }
        });
        executorService.execute(()->{
            while (count < defaultValue){
                if(!flag) {
                    System.out.println("num is " + count);
                    count++;
                    flag = true;
                }
            }
        });
    }

}
